package collision;

import java.awt.Event;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.media.j3d.WakeupOr;

public class CollisionBehavior extends Behavior {

	private TransformGroup g;	
	private WakeupOnCollisionEntry wEnter;
	private WakeupOnCollisionExit wExit;
	
	private WakeupCriterion mousePressed;
	
	private boolean collision = false;
  	
//	private WakeupCriterion[] criterion = new WakeupCriterion[3];
  	/*WakeupCriterion criterion[]={
  			mousePressed,
  			wEnter,
  			wExit
  	};*/

  	private WakeupCondition conditions = null;
  	
  	public CollisionBehavior(){
  		super();
  		
  		
  	}
  	
  	public void setTransformGroup(TransformGroup g){
  		this.g = g; 		
  		wEnter=new WakeupOnCollisionEntry(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		wExit=new WakeupOnCollisionExit(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		mousePressed = new WakeupOnAWTEvent(Event.MOUSE_DOWN);
  		WakeupCriterion[] criterion = new WakeupCriterion[3];
  		criterion[0] = mousePressed;
  		criterion[1] = wEnter;
  		criterion[2] = wExit;
  		conditions=new WakeupOr(criterion);	 
  		System.out.println("conditions changed");
  	}
  	
  	public boolean getCollision(){
  		return this.collision;
  	}
  	
	@Override
	public void initialize() {
		
		//wEnter=new WakeupOnCollisionEntry(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		//wExit=new WakeupOnCollisionExit(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		mousePressed = new WakeupOnAWTEvent(Event.MOUSE_DOWN);
  		
  		WakeupCriterion[] criterion = new WakeupCriterion[3];
  		criterion[0] = mousePressed;
  		criterion[1] = wEnter;
  		criterion[2] = wExit;
  		conditions=new WakeupOr(criterion);	 
  		this.wakeupOn(conditions);
  		
  		
  		//wakeupOn(conditions);
  		//System.out.println("detect inside");
	}

	@Override
	public void processStimulus(Enumeration criteria) {
		// TODO Auto-generated method stub
		//wEnter=new WakeupOnCollisionEntry(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		//wExit=new WakeupOnCollisionExit(g,WakeupOnCollisionEntry.USE_GEOMETRY);
  		System.out.println("process collision detect");
		WakeupCriterion wakeup;
     	while( criteria.hasMoreElements()){
     		wakeup=(WakeupCriterion)criteria.nextElement();
     		
     		if(wakeup instanceof WakeupOnCollisionEntry){
     		    System.out.println("enter collision");
     		    collision = true;
     		} 
     		else if(wakeup instanceof WakeupOnCollisionExit){
     			System.out.println("exit collision");
     			collision = false;
     		}
     		wakeupOn(conditions);
     		
		
     	}
	}

}
